/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businessthread;

/**
 *
 * @author TQNINH
 */
public class BusinessAlive extends Thread {

    String name;
    Thread objTh;
    
    public BusinessAlive(String str){
        name = str;
        objTh = new Thread(this, name);
        System.out.println("New Threads are starting: " + objTh);
        objTh.start();
    }
    
    public void run(){
        try{
            for(int count = 0;count < 2;count++){
                System.out.println(name + ": " + count);
                objTh.sleep(3000);
            }
        } catch(InterruptedException e) {
            System.out.println(name + " interrupted.");
        }
        System.out.println(name + " exiting.");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        BusinessAlive td1 = new BusinessAlive("Meeting Deadlines");
        BusinessAlive td2 = new BusinessAlive("Schedule Variance");
        BusinessAlive td3 = new BusinessAlive("Effort Variance");
        
        System.out.println("First thread is alive: " + td1.objTh.isAlive());
        System.out.println("Second thread is alive: " + td2.objTh.isAlive());
        System.out.println("Third thread is alive: " + td3.objTh.isAlive());
        
        System.out.println("\nWe are in the main and waiting for the business " +
                "threads to finish.\n");
        
        try{
            td1.objTh.join();
            td2.objTh.join();
            td3.objTh.join();
        } catch(InterruptedException ex) {
            System.out.println("Main thread is interrupted.");
        }
        
        System.out.println("First thread is alive: " + td1.objTh.isAlive());
        System.out.println("Second thread is alive: " + td2.objTh.isAlive());
        System.out.println("Third thread is alive: " + td3.objTh.isAlive());
        System.out.println("\nMain thread is over and exiting.");
    }
}
